package com.kzd.btgame.main.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.LogUtils
import com.kzd.btgame.app.AppFlowBus
import com.kzd.btgame.callback.AnimationOnScrollListener
import com.kzd.btgame.custom.createViewTextBottom
import com.kzd.btgame.custom.setTransitionStatusE
import com.kzd.btgame.databinding.IncludeAppRefreshBinding
import com.kzd.btgame.entity.ReserveChangeResponse
import com.kzd.btgame.jetpack.vm.HomeViewModel
import com.kzd.btgame.main.*
import com.kzd.btgame.main.home.adapter.HomeFeaturedAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class HomeFeaturedFragment : OwnerViewBindingFragment<IncludeAppRefreshBinding>(){

    companion object {
        fun newInstance(): HomeFeaturedFragment {
            return HomeFeaturedFragment()
        }
    }

    private val mHomeFeaturedAdapter = HomeFeaturedAdapter()

    private val mFooterViewBottom: View by lazy {
        createViewTextBottom(context = requireActivity(), "更多游戏请到游戏大厅中查看")
    }

    val mViewModel: HomeViewModel by lazy {
        ownerViewModel(requireParentFragment())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> IncludeAppRefreshBinding
        get() = IncludeAppRefreshBinding::inflate

    override fun initView() {

        mViewBinding.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mHomeFeaturedAdapter

        mHomeFeaturedAdapter.setOnItemClickListener { _, _, position ->
            val itemData = mHomeFeaturedAdapter.getItem(position)
            if (!itemData.game_layout.isNullOrEmpty()) {
                mViewModel.homeDoings(itemData.game_layout)
            } else if (itemData.type == 3 || itemData.type == 4) {
                val index = when (itemData.type) {
                    3 -> 0
                    4 -> 2
                    else -> 0
                }
                toGameTravelActivity(index)
            }
        }

        mHomeFeaturedAdapter.setOnItemHomeNav { _, nav ->
            if (!nav.game_layout.isNullOrEmpty()) {
                mViewModel.homeDoings(nav.game_layout)
            } else if (!nav.page.isNullOrEmpty()) {
                if (nav.page == HomeLotteryActivity::class.java.name) {
                    mViewModel.homeLottery()
                } else {
                    toActivity(nav.page, nav.login)
                }
            } else if (nav.type == 1) {
                toGameTravelActivity()
            } else if (nav.type == 3) {
                toHomeCouponActivity()
            } else if (nav.type == 4) {
                toHomeServiceActivity()
            } else if (nav.type == 5) {
                toHomeBargainActivity()
            } else if (nav.type == 6) {
                toHomePresentActivity()
            }
        }

//        mHomeFeaturedAdapter.setOnItemReserve { _, position, reserve ->
//            val gid = reserve.id
//                ?: return@setOnItemReserve
//            val type = if (reserve.is_yuyue)
//                "1" else "0"
//            checkLogin {
//                mViewModel.reserveChange(gid, type, position)
//            }
//        }

        mViewBinding.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.homeIndex()
        }
    }


    override fun initData() {
        mViewModel.homeIndex()
    }


    override fun initViewMode() {
        mViewModel.indexHomeLiveData.observe(viewLifecycleOwner, { response ->
            setTransitionStatusE(response,
                mHomeFeaturedAdapter,
                mViewBinding.mSmartRefreshLayout,
                (false))
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mHomeFeaturedAdapter.recyclerView
                .setItemViewCacheSize(data.size)
            mHomeFeaturedAdapter.setNewInstance(data)
            mHomeFeaturedAdapter.setFooterView(mFooterViewBottom)
        })

        mViewModel.reserveChangeLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val position = response.data?.position
                ?: return@observe
            val data = response.data?.data
                ?: return@observe
            mHomeFeaturedAdapter.setGame6AdapterData(position, data)
        })

        mViewModel.homeDoingsLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            toHomeDoingsActivity(data.top.title, mViewModel.mGameLayout, data)
        })

        mViewModel.homeLotteryLiveData.observe(viewLifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                toast(response.error?.msg)
            }
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            toHomeLotteryActivity(data)
        })
    }

    override fun onResume() {
        super.onResume()
        mHomeFeaturedAdapter.startBannerViewSwipe()
    }

    override fun onPause() {
        super.onPause()
        mHomeFeaturedAdapter.stopBannerViewSwipe()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mHomeFeaturedAdapter.destroyBannerViewSwipe()
    }
}